﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Reticulum
{
    public partial class Delete : Form
    {
        
        public Delete()
        {
            InitializeComponent();
        }

        private void Delete_Load(object sender, EventArgs e)
        {
            //this.Height = 500;
            //this.Width = 800;
            
            string sql = "select UserName,UserAccount,UserPwd,UserPhone,Sex,BloodGroup,Birthday,Occupational," +
                "Company,Area,Hometown,Email,UploadState,DownloadState from Users";

            DataSet DS = DBHelps.Adapter(sql);

            dataGridView1.DataSource = DS.Tables[0];
            //LoginHome.SQL.Close();
            //LoginHome.SQL.Dispose();释放资源
            dataGridView1.ContextMenuStrip = contextMenuStrip1;
        }

        int index = 0;

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.dataGridView1.Rows[e.RowIndex].Selected = true;//是否选中当前行
                index = e.RowIndex;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];

                //每次选中行都刷新到datagridview中的活动单元格
                this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
                //指定控件（DataGridView），指定位置（鼠标指定位置）
                this.contextMenuStrip1.Show(Cursor.Position);//锁定右键列表出现的位置
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string account = dataGridView1.Rows[index].Cells["UserAccount"].Value.ToString();//获取选中行的账号
            string name = dataGridView1.Rows[index].Cells["ColName"/*这是dataGridView列里的名*/].Value.ToString();//获取用户名
            if (!this.dataGridView1.Rows[index].IsNewRow)//判断是否为新行
            {
                DialogResult Dr = MessageBox.Show("是否确定删除："+name+"这位用户信息？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if (Dr==DialogResult.OK)
                {
                    string deletestr = string.Format("delete from Users where UserAccount='{0}'", account);
                    bool result = DBHelps.Command(deletestr);
                    if (result)
                    {
                        MessageBox.Show(name + "该用户已成功删除！");
                        Delete_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
            else
            {
                MessageBox.Show("请删除有效数据！");
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete_Load(sender, e);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("账号和密码都要输入！");
            }
            else if (textBox1 != null && textBox2 != null)
            {
                DialogResult result;
                result = MessageBox.Show("确定要删除吗？", "", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    string sql = string.Format("delete from Users where UserAccount='{0}' and UserPwd='{1}'", textBox1.Text, textBox2.Text);
                    bool num = DBHelps.Command(sql);
                    if (num)
                    {
                        MessageBox.Show("删除成功！");
                        Delete_Load(sender, e);//更新数据
                    }
                    else
                    {
                        MessageBox.Show("删除失败！可能输入有误！");
                    }
                }
            }
        }
    }
}
